package com.foosun.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.foosun.core.entity.User;
import com.foosun.core.filter.SessionFilter;
import com.foosun.core.service.UserService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class MainController {

    @Resource
    private UserService userService;


    @RequestMapping("/login.html")
    public String login() throws Exception {
        return "/login";
    }

    @RequestMapping("/logout.html")
    public String ExitProcess(HttpSession session) {
        session.invalidate();
        return "redirect:/login.html";
    }


    @RequestMapping("/main.html")
    public String login(Model model, HttpSession session, String userName, String password) throws Exception {
        User user = (User) session.getAttribute(SessionFilter.USER);
        if (user == null) {
            if (StringUtils.isBlank(userName)) {
                return "redirect:/login.html";
            } else {
                user = userService.login(userName, password);
                if (user == null) {
                    String message = "用户名或密码错误！";
                    model.addAttribute("message", message);
                    model.addAttribute("userName", userName);
                    return "/login";
                } else {
                    if (user.getEnable() != null && user.getEnable()) {
                        String message = "用户已被禁用！";
                        model.addAttribute("message", message);
                        return "/login";
                    }
                    session.setAttribute(SessionFilter.USER, user);
                }
            }
        }

        return "/main";
    }

    @RequestMapping("/login.flex")
    @ResponseBody
    public JSONObject _login(Model model, HttpSession session, String userName, String password) throws Exception {
        JSONObject result = new JSONObject();
        User user = (User) session.getAttribute(SessionFilter.USER);
        if (user == null) {
            if (StringUtils.isBlank(userName)) {
                result.put("isLogin", false);
                result.put("message", "请登录！");
                result.put("status", 202);
            } else {
                user = userService.login(userName, password);
                if (user == null) {
                    result.put("isLogin", false);
                    result.put("message", "用户名或密码错误！");
                    result.put("status", 202);
                } else if (user.getEnable() != null && user.getEnable()) {
                    result.put("isLogin", false);
                    result.put("message", "用户已被禁用！");
                    result.put("status", 202);
                } else {
                    result.put("isLogin", true);
                    result.put("message", "登录成功！");
                    result.put("status", 200);
                    result.put("userId", user.getId());
                    result.put("jsessionId", session.getId());
                }
            }
        }
        System.out.println("login session = " + session.getId());
        return result;
    }

    @RequestMapping("/get_user.flex")
    @ResponseBody
    public JSONObject _getUser(HttpSession session, Integer userId) throws Exception {
        JSONObject result = new JSONObject();

        JSONObject u = null;
        if (userId != null && userId > 0) {
            User user = userService.findById(userId);
            if (user != null) {
                u = new JSONObject();
                u.put("userName",user.getUserName());
                u.put("nickName",user.getNickName());
                u.put("roleName",user.getMemo());

            }
        }
        result.put("user", u);
        System.out.println("get_user session = " + session.getId());
        return result;
    }

    @RequestMapping("/main2.html")
    public String login2(Model model, HttpSession session, String userName, String password) throws Exception {
        User user = (User) session.getAttribute(SessionFilter.USER);
        if (user == null) {
            if (StringUtils.isBlank(userName)) {
                return "redirect:/login.html";
            } else {
                user = userService.login(userName, password);
                if (user == null) {
                    String message = "用户名或密码错误！";
                    model.addAttribute("message", message);
                    model.addAttribute("userName", userName);
                    return "/login";
                } else {
                    if (user.getEnable() != null && user.getEnable()) {
                        String message = "用户已被禁用！";
                        model.addAttribute("message", message);
                        return "/login";
                    }
                    session.setAttribute(SessionFilter.USER, user);
                }
            }
        }

        return "/common/main";
    }


}
